﻿function MachineManagerModel() {
    self = this;

    //Declare observable which will be bind with UI
    self.Id = ko.observable("");
    self.Name = ko.observable("");
    self.Enabled = ko.observable("");
    self.Description = ko.observable("");

    var Machine = {
        Id: self.Id,
        Name: self.Name,
        Enabled: self.Enabled,
        Description: self.Description
    };

    // Initialize the view-model
    $.ajax({
        url: '@Url.Action("GetAllMachines", "Machine")',
        cache: false,
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        data: {},
        success: function (data) {
            self.machines(data); //Put the response in ObservableArray
        }
    });

    self.machines = ko.observableArray();//// Contains the list of products
    self.Machine = ko.observable();

    //Add New Item
    self.create = function () {
        if (Machine.Name() != "") {
            $.ajax({
                url: '@Url.Action("AddMachine", "Machine")',
                cache: false,
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: ko.toJSON(Machine),
                success: function (data) {
                    // alert('added');
                    self.machines.push(data);
                    self.Name("");
                    self.Enabled("");
                    self.Description("");
                }
            }).fail(
                 function (xhr, textStatus, err) {
                     alert(err);
                 });
        }
        else {
            alert('Please Enter All the Values !!');
        }

    }

    // Delete product details
    self.delete = function (Machine) {
        if (confirm('Are you sure to Delete "' + Machine.Name + '" machine ??')) {
            $.ajax({
                url: '@Url.Action("DeleteMachine", "Machine")',
                cache: false,
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: ko.toJSON(Machine),
                success: function (data) {
                    self.machines.remove(Machine);
                    //   alert("Record Deleted Successfully");
                }
            }).fail(
             function (xhr, textStatus, err) {
                 alert(err);
                 alert(xhr);
                 alert(textStatus);
             });
        }
    }

    // Edit product details
    self.edit = function (Machine) {
        self.Machine(Machine);
    }

    // Update product details
    self.update = function () {
        var machine = self.Machine();

        $.ajax({
            url: '@Url.Action("EditMachine", "Machine")',
            cache: false,
            type: 'PUT',
            contentType: 'application/json; charset=utf-8',
            data: ko.toJSON(Machine),
            success: function (data) {
                self.machines.removeAll();
                self.machines(data); //Put the response in ObservableArray
                self.Machine(null);
                alert("Record Updated Successfully");
            }
        })
    .fail(
    function (xhr, textStatus, err) {
        alert(err);
    });
    }


    // Reset product details
    self.reset = function () {
        self.Name("");
        self.Enabled("");
        self.Description("");
    }

    // Cancel product details
    self.cancel = function () {
        self.Machine(null);

    }
}

var viewModel = new MachineManagerModel();
ko.applyBindings(viewModel);